﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginarium.team;
using GuiLibrary;
using GuiLibrary.Components;
using GuiLibrary.Layouts;
using Microsoft.Xna.Framework;
using GameEngine.Scenes;
using Microsoft.Xna.Framework.Graphics;
using GameEngine.animations;
using Imaginarium.battleSistem;
using GuiLibrary.layouts;

namespace Imaginarium.gui
{
    public class SelectTargetGui: Gui, IOnCancel
    {
        public List<Team> targetsTeams { get; set; }
        //TO do constructors
        
        
        public SelectTargetGui(List<Team> teams):
            base()
        {
            targetsTeams = teams;
        }

        public override void LoadContent()
        {
            //TODO Cambiar background
            basePanel = new Panel(position, GUIParameters.backgroundSelTargetGUI());
            
            SpriteFont font = Imaginarium.ImaginariumGame.getInstance().Content.Load<SpriteFont>(CodeParameters.selectSkillFont);
            foreach (Team t in targetsTeams) {
                List<Character> characters = t.characters;
                List<Element> elements = new List<Element>();
                Panel panel = new Panel(basePanel);
                foreach (Character c in characters) {
                    if(!c.isDead() || !c.dissapearAtDying)
                        panel.components.Add( new SelectableLabel(panel, new Vector2(), new Text(font,
                            c.name + (c.isDead() ? " " + CodeParameters.deadText : ""), (c.isDead() ? Color.DarkRed : Color.White), OriginType.LEFT_UP), new ImaginariumElement(c)));                    

                }
                panel.layout = new ListLayout(panel, GUIParameters.marginTargetSelectBasePanel, true, (t.isUser)?this:null, this, null);
                basePanel.components.Add(panel);
            }
            basePanel.layout = new ListLayoutMultiPanel(basePanel, GUIParameters.marginsZERO, false, this, this, null);
            basePanel.layout.refresh();
            basePanel.moveCursor(Layout.Cursor.RIGHT); basePanel.moveCursor(Layout.Cursor.LEFT); //this is to clean
        }


        public override void onSelectedElement(Element e)
        {
            BattleManager.getInstance().turnExpert.setTargetCommand((Character)e.associatedObject);
            removeScenes();
            BattleManager.getInstance().turnExpert.startNewBattleCommand();
            BattleManager.getInstance().turnExpert.onCharacterTakenAction();
        }

        private void removeScenes() {
            int howMany = 0;
            switch (BattleManager.getInstance().turnExpert.getBattleCommandType()) { 
                case BattleOption.BattleOptionEnum.Ability:
                    howMany = 3;
                    break;
                case BattleOption.BattleOptionEnum.Attack:
                    howMany = 2;
                    break;
                case BattleOption.BattleOptionEnum.Inventory:
                    howMany = 3;
                    break;
                case BattleOption.BattleOptionEnum.Defend:
                    howMany = 1;
                    break;
            };
            for (int i = 1; i <= howMany; i++)
            {
                SceneManager.getInstance().markForRemove(SceneManager.getInstance().scenes.ElementAt(SceneManager.getInstance().scenes.Count - i));
            }
        }

        public void onCancel() {
            SceneManager.getInstance().markForRemove(this);
        }
    }
}
